<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    button.btn{
      position: fixed;
      right: 50px;
      bottom: 50px;
    }
    button.a{
      position: fixed;
      right: 50px;
      top: 50px;
    }
  </style>
</head>
<body style="height: 3000px;">
  <div id="app">
    <button class="a"  @click="isShow = false">隐藏</button>
    <button class="btn" v-backtop="200" v-if="isShow">回到顶部</button>
  </div>
  <script src="./vue.js"></script>
  <script>
    Vue.directive('backtop',{
      inserted(el,binding){
       el.style.display = "none";
       window.onscroll = ()=>{
         console.log(111)
         let sTop = document.documentElement.scrollTop||document.body.scrollTop;
         if(sTop>binding.value){
          el.style.display = "block";
         }else{
          el.style.display = "none";
         }
       }

       el.onclick = function(){
         window.scrollTo(0,0)
       }
      },
      unbind(){
        console.log('指令解绑了')
        window.onscroll = null;
      }
    })
  
    const vm = new Vue({
      el:'#app',
      data:{
        isShow: true
      }
    })
  </script>
</body>
</html>